package com.projectx.action.talk;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.adpter.ChatRecordAdapter;
import com.projectx.entity.LastChatRecord;

import java.util.ArrayList;
import java.util.List;

public class CurrentTalkFragment extends Fragment implements UpdateCurrentTalk{
    List<LastChatRecord> chatRecords = new ArrayList<>();
    LastChatRecord lastChatRecord = new LastChatRecord();
    Context context;
    RecyclerView recyclerView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        if (context != null){
            lastChatRecord.setAccountName("mikumiku");//这是别人的账户名
            lastChatRecord.setLastRecord("who");
            chatRecords.add(lastChatRecord);
            View view = inflater.inflate(R.layout.fragment_current_talk,container,false);
            recyclerView = view.findViewById(R.id.recycle_view_current_talk);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setStackFromEnd(true);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(new ChatRecordAdapter(getActivity(),chatRecords,getActivity().getIntent().getStringExtra("accountName")));
            recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
            return view;
        }
        return inflater.inflate(R.layout.fragment_current_talk, container, false);
    }

    @Override
    public boolean deleteAll() {
        chatRecords.clear();
        if (recyclerView.getAdapter() != null){
            recyclerView.getAdapter().notifyDataSetChanged();
            return true;
        }
        return false;
    }
}

